package cn.sciento.starter.social.core.security;

import javax.servlet.http.HttpServletRequest;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.web.HttpSessionSessionStrategy;
import org.springframework.social.connect.web.ProviderSignInAttempt;
import org.springframework.social.connect.web.SessionStrategy;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

public class ProviderBindHelper {
  public static final String SESSION_ATTRIBUTE = ProviderBindHelper.class.getName();
  
  private static SessionStrategy sessionStrategy = (SessionStrategy)new HttpSessionSessionStrategy();
  
  public static void setConnection(HttpServletRequest request, Connection connection) {
    sessionStrategy.setAttribute((RequestAttributes)new ServletWebRequest(request), ProviderSignInAttempt.SESSION_ATTRIBUTE, connection);
  }
  
  public static Connection<?> getConnection(HttpServletRequest request) {
    return (Connection)sessionStrategy.getAttribute(new ServletWebRequest(request), ProviderSignInAttempt.SESSION_ATTRIBUTE);
  }
  
  public static void removeConnection(HttpServletRequest request) {
    sessionStrategy.removeAttribute((RequestAttributes)new ServletWebRequest(request), ProviderSignInAttempt.SESSION_ATTRIBUTE);
  }
}
